var PurOrgRowList = function() {

	var left_margin_step = 15;
	var currentPurOrgObj=null;



	//"close"样式(-)是已经展开时的样式
	// 控制方法：
	// 如果当前节点尚未展开(+)，此次点击触发下列行为：(1)将当前节点样式设置为已展开；(2)从后台查数据，渲染子节点;(3)将同辈的其他已展开节点及其子节点收缩，且将收缩节点的样式设置为收缩样式
	// 如果当前节点已经展开(-)，此次点击触发下列行为：(1)将当前节点的子节点收缩；(2)当前节点的样式设置为展开(-)
	$(".category_trigger8").live(
			'click',
			function() {
				var category_triggerObj=$(this);
				if(category_triggerObj.hasClass("close")){
					category_triggerObj.parent(".tree_category8").next(".children").slideUp(function(){
						category_triggerObj.removeClass("close");
					});
					return false;
				}
				
				$(this).addClass("close");
				var pk_purorg_val = category_triggerObj.next("input").val();
				
				var myts = new Date();
				jQuery.ajax( {
					type : "GET",
					async : false,
					url : ebvp_context
							+ "/other/qryPurOrgByPk?pk_purorg="
							+ pk_purorg_val 
							+ "&myts=" + myts,
					dataType : "text",
					success : function(result) {
						
						
						var orgArr = eval(result);
						
						var current_margin_left_size = parseInt(category_triggerObj.attr("margin")) + left_margin_step;	
						
						var childrenDiv = $("<div></div>").addClass("children");
						for ( var loop = 0; loop < orgArr.length; loop++) {
							var obj = orgArr[loop];

							var a = $("<a></a>").attr("href", "#").addClass(
									"category_trigger8").attr("style","margin-left:"+current_margin_left_size+"px").attr("margin", current_margin_left_size);
							var inputObj = $("<input/>").val(obj.pk_org).attr(
									"type", "radio").attr("name", "pk_purOrg_intree");
							
							var label = $("<label></label>").text(obj.name);
							var inputObj1 = $("<input/>").val(obj.pk_org).attr(
									"type", "hidden");	

							var div = $("<div></div>")
									.addClass("tree_category8").append(a)
									.append(inputObj).append(label).append(inputObj1);
											
							childrenDiv.append(div);
						}
						childrenDiv.insertAfter(category_triggerObj.parent());
					}
				});
				
				//自身展开后，将其兄弟节点及其子节点收缩回
				category_triggerObj.parent(".tree_category8").next(".children").slideDown();
				
				if(category_triggerObj.parent(".tree_category8").length>0&&category_triggerObj.parent(".tree_category8").siblings(".tree_category8").length>0){
				 category_triggerObj.parent(".tree_category8").siblings(".tree_category8").next(".children").slideUp(function(){
					 $(this).prev(".tree_category8").find(".category_trigger8").removeClass("close");
				 });
				}
				return false;

			});
	
	$(".tree_btns8 .idBtnPicked").live('click', function() {
		var items = $(this).parents(".ui-drop_dialog").find("[name='pk_purOrg_intree']");
		if(items != null){
			var str="";
			var pk="";
			for(var loop=0; loop<items.length; loop++){
				if(items[loop].checked == true){					
					str += $(items[loop]).next("label").html() ;
					pk += $(items[loop]).next("label").next("input").val();
					break;
				}
			}
			currentPurOrgObj.val(str);
			currentPurOrgObj.next("input").val(pk);
			
		}
		
		$(this).parents(".ui-drop_dialog").slideUp();
		return 0;
		
	});
	
	$(".tree_btns8 .idBtnCancel").live('click', function(){
		$(this).parents(".ui-drop_dialog").slideUp();
	})

	this.init = function() {

		$("body").delegate(
				"[name='pur_org']",
				"click",
				function() {
					
					currentPurOrgObj = $(this);
										
					var myts = new Date();
					jQuery.ajax( {
						type : "GET",
						async : false,
						url : ebvp_context + "/other/qryPurOrg?myts=" + myts,
						dataType : "text",
						success : function(result) {
							if(result != "[]"){
								var orgArr = eval(result);
								$("#purOrgRowPicker").find(".tree_list8").empty();
								for ( var loop = 0; loop < orgArr.length; loop++) {
									var obj = orgArr[loop];
									var a = $("<a></a>").attr("href", "#").addClass(
											"category_trigger8").attr("margin", 0);								
									var inputObj = $("<input/>").val(obj.pk_org).attr(
											"type", "radio").attr("name", "pk_purOrg_intree");	
									var label = $("<label></label>").text(obj.name);
									var inputObj1 = $("<input/>").val(obj.pk_org).attr(
											"type", "hidden");	
									var div = $("<div></div>")
											.addClass("tree_category8").append(a)
											.append(inputObj).append(label).append(inputObj1);												
									$("#purOrgRowPicker").find(".tree_list8")
											.append(div);
								}
							}
						}				
					})
				});
		
	}
}

$(function() {
	var purOrgRow = new PurOrgRowList();
	purOrgRow.init();
});
